<?php
/**
 * Cruds Controller
 */
namespace PUF\Plugin;

class Cruds extends \PUF\Controller {
	
	
	protected function _before() {
		parent::_before();
		$this->View->setTitle('Administración - ABMs');
		
		$Loader = \PUF\Loader::getInstance();
		$Loader->load('Crud', ['type' => 'model', 'plugin' => 'Admin']);
		$this->View->setLayout('default', ['plugin' => 'Admin']);
	}
	
	public function index() {
		$this->View->setView('Cruds/listing', ['plugin' => 'Admin']);
		
		$cruds = Crud::find();
		$this->setVar('cruds', $cruds);
	}
	
	public function edit($id = null) {
		$this->View->setView('Cruds/edit', ['plugin' => 'Admin']);
		$crud = Crud::findBy('id', $id);
		
		if (!$crud) {
			$crud = new Crud();
		}
		
		if (isset($_REQUEST['id'])) {
			$crud->setFromRequest();
			$crud->save();
		}

		$this->setVar('crud', $crud);
	}
}